package com.wt.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wt.admin.entity.WqunBlog;
import com.wt.admin.entity.WqunOOS;
import com.wt.admin.entity.WqunProject;
import com.wt.admin.service.WqunBlogService;
import com.wt.admin.service.WqunProjectService;
import com.wt.commonutils.R;
import com.wt.oos.Upload;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

/**
 * <p>
 * 项目表 前端控制器
 * </p>
 *
 * @author wt
 * @since 2023-09-24
 */
@Api(tags = "项目管理")
@CrossOrigin
@RestController
@RequestMapping("/wqunProject")
public class WqunProjectController {

    @Autowired
    private WqunProjectService wqunProjectService;

    //查询所有项目数量
    @ApiOperation("查询所有项目数量")
    @GetMapping("/allProjectNum")
    @PreAuthorize("hasAnyAuthority('wqunProject:allProjectNum')")
    public R allProjectNum(){
        QueryWrapper<WqunProject> queryWrapper = new QueryWrapper();
        queryWrapper.eq("status",1);
        int allProjectNum = wqunProjectService.count(queryWrapper);
        return R.ok().data("allProjectNum",allProjectNum);
    }

    //查询所有文章
    @ApiOperation("查询所有项目")
    @GetMapping("/findAllProject")
    @PreAuthorize("hasAnyAuthority('admin:wqunProject:findAllProject')")
    public R findAllProject(){
        QueryWrapper<WqunProject> queryWrapper = new QueryWrapper();
        queryWrapper.orderByDesc("created_time");
        queryWrapper.eq("status",1);
        queryWrapper.select("id","title","summary","download_count","outside_link","file_uid","created_time");
       List<WqunProject> findAllProject = wqunProjectService.list(queryWrapper);
        return R.ok().data("findAllProject",findAllProject);
    }

    //查询id项目
    @ApiOperation("查询id项目")
    @GetMapping("/findProjectById/{id}")
    @PreAuthorize("hasAnyAuthority('admin:wqunProject:findProjectById')")
    public R findProjectById(@PathVariable("id") String id){
        QueryWrapper<WqunProject> queryWrapper = new QueryWrapper();
        queryWrapper.orderByDesc("created_time");
        queryWrapper.eq("id",id);
        queryWrapper.eq("status",1);
        queryWrapper.select("id","title","summary","download_count","outside_link","file_uid","created_time");
        WqunProject findProjectById = wqunProjectService.getOne(queryWrapper);

        return findProjectById!=null? R.ok().data("findProjectById",findProjectById):R.error();
    }

    //修改项目
    @ApiOperation("修改项目")
    @PostMapping("/updateProject")
    @PreAuthorize("hasAnyAuthority('admin:wqunProject:updateProject')")
    public R updateProject(@RequestBody WqunProject wqunProject){
        QueryWrapper<WqunProject> queryWrapper = new QueryWrapper<>();
        WqunProject byId = wqunProjectService.getById(wqunProject.getId());

        //项目修改
        boolean b1 = false;
        if(!Objects.equals(byId.getOutsideLink(), wqunProject.getOutsideLink())){
            b1 = Upload.deleteUploadProject(byId.getOutsideLink());
        }

        boolean b2 = false;
        if(!Objects.equals(byId.getFileUid(), wqunProject.getFileUid())){
            b1 = Upload.deleteUploadImg(byId.getFileUid());
        }

        boolean b = wqunProjectService.updateById(wqunProject);
        return  b ?R.ok().message("修改成功"):R.error().message("修改失败");
    }


    //添加项目
    @ApiOperation("添加项目")
    @PostMapping("/addProject")
    @PreAuthorize("hasAnyAuthority('admin:wqunProject:addProject')")
    public R addProject(@RequestBody WqunProject wqunProject){
//        System.out.println("wqunProject = " + wqunProject);
        boolean b = wqunProjectService.save(wqunProject);
        return  b?R.ok().message("添加成功"):R.error().message("添加失败");
    }

    //删除项目
    @ApiOperation("删除项目")
    @GetMapping("/deleteProject/{id}")
    @PreAuthorize("hasAnyAuthority('admin:wqunProject:deleteProject')")
    public R deleteProject(@PathVariable("id") String id){
        WqunProject byId = wqunProjectService.getById(id);
        byId.setStatus(0);
        boolean b = wqunProjectService.updateById(byId);
        return  b?R.ok().message("删除成功"):R.error().message("删除失败");
    }

}

